﻿Public Class frm_smk_add

    Dim i As Integer


    Sub HitungNilai()
        Try
            Dim n_indo, n_ingg, n_mate, n_ipa, n_agama, n_pkn, n_ips As Double
            Dim total As Double

            n_indo = Pengkali_UN * CDbl(txt_n_indonesia.Text)
            n_ingg = Pengkali_UN * CDbl(txt_n_inggris.Text)
            n_mate = Pengkali_UN * CDbl(txt_n_matematika.Text)
            n_ipa = Pengkali_UN * CDbl(txt_n_ipa.Text)

            n_agama = Pengkali_US * CDbl(txt_n_agama.Text)
            n_pkn = Pengkali_US * CDbl(txt_n_pkn.Text)
            n_ips = Pengkali_US * CDbl(txt_n_ips.Text)

            total = n_indo + n_ingg + n_mate + n_ipa + n_agama + n_pkn + n_ips

            txt_n_total.Text = CStr(FormatNumber(total, 2))

        Catch ex As Exception
            txt_n_total.Text = "0,00"
        End Try
    End Sub

    Sub bersihkan()
        cmb_asal_pendaftar.SelectedText = 0
        txt_no_pendaftar_02.Text = vbNullString
        cmb_tahun_lulus.SelectedIndex = 0
        mask_no_un.Text = vbNullString
        txt_nama_pendaftar.Text = vbNullString
        cmb_jenis_kelamin.SelectedIndex = 0
        txt_tempat_lahir.Text = vbNullString
        mask_tanggal_lahir.Text = vbNullString
        txt_asal_sekolah.Text = vbNullString

        txt_n_indonesia.Text = "0,00"
        txt_n_inggris.Text = "0,00"
        txt_n_ipa.Text = "0,00"
        txt_n_matematika.Text = "0,00"
        txt_n_agama.Text = "0,00"
        txt_n_ips.Text = "0,00"
        txt_n_pkn.Text = "0,00"

        txt_n_total.Text = "0,00"

        cmb_pilihan_2.Text = ListSekolah.getNama(APP.Info.KD_Sekolah)

        cmb_asal_pendaftar.Focus()

    End Sub

    Function validasi_nilai() As Boolean
        If CInt(txt_n_indonesia.Text) > 10 Or CInt(txt_n_indonesia.Text) <= 0 Then
            MsgBox("Isilah Nilai B.Indonesia Dengan Benar")
            txt_n_indonesia.Focus()
            Return False
        End If
        If CInt(txt_n_inggris.Text) > 10 Or CInt(txt_n_inggris.Text) <= 0 Then
            MsgBox("Isilah Nilai B.Inggris Dengan Benar")
            txt_n_inggris.Focus()
            Return False
        End If

        If CInt(txt_n_ipa.Text) > 10 Or CInt(txt_n_ipa.Text) <= 0 Then
            MsgBox("Isilah Nilai IPA Dengan Benar")
            txt_n_ipa.Focus()
            Return False
        End If

        If CInt(txt_n_matematika.Text) > 10 Or CInt(txt_n_matematika.Text) <= 0 Then
            MsgBox("Isilah Nilai Matematika Dengan Benar")
            txt_n_matematika.Focus()
            Return False
        End If

        If CInt(txt_n_agama.Text) > 10 Or CInt(txt_n_agama.Text) <= 0 Then
            MsgBox("Isilah Nilai Agama Dengan Benar")
            txt_n_agama.Focus()
            Return False
        End If

        If CInt(txt_n_ips.Text) > 10 Or CInt(txt_n_ips.Text) <= 0 Then
            MsgBox("Isilah Nilai IPS Dengan Benar")
            txt_n_ips.Focus()
            Return False
        End If

        If CInt(txt_n_pkn.Text) > 10 Or CInt(txt_n_pkn.Text) <= 0 Then
            MsgBox("Isilah Nilai PKn Dengan Benar")
            txt_n_pkn.Focus()
            Return False
        End If

        Return True

    End Function


    Private Sub cmd_simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_simpan.Click

        '# cek no pendaftaran
        DB.FlushCache()
        DB.Selects("*")
        DB.From("tbl_pendaftaran")
        DB.Where("no_pendaftaran", txt_no_pendaftar_01.Text & "-" & txt_no_pendaftar_02.Text)

        Connection.ExecuteToDataReader(DB.GetQueryString)

        If Connection.Read.HasRows Then
            MsgBox("Sudah terdapat no pendaftaran '" & txt_no_pendaftar_01.Text & "-" & txt_no_pendaftar_02.Text & "'" & vbCrLf & "Ganti nomor pendaftaran....", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
            Exit Sub
        End If

        '# cek no UN
        DB.FlushCache()
        DB.Selects("*")
        DB.From("tbl_pendaftaran")
        DB.Where("no_uan", mask_no_un.Text)

        Connection.ExecuteToDataReader(DB.GetQueryString)

        If Connection.Read.HasRows Then
            MsgBox("Sudah terdapat no UN '" & mask_no_un.Text & "'" & vbCrLf & "Ganti nomor UN....", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
            Exit Sub
        End If

        '# vaidasi nilai
        If validasi_nilai() = False Then
            Exit Sub
        End If

        '# Validasi
        Validation.clearRules()
        Validation.addRules(txt_no_pendaftar_02.Text, "Nomer Pendaftaran", "required|numeric|length[4-4]")
        Validation.addRules(mask_no_un.Text.Replace("_", vbNullString), "Nomor UN", "length[12-12]")
        Validation.addRules(txt_tempat_lahir.Text, "Tempat Lahir", "required|length[1-20]")
        Validation.addRules(mask_tanggal_lahir.Text, "Tanggal Lahir", "date")
        Validation.addRules(txt_asal_sekolah.Text, "Asal Sekolah", "required|length[1-100]")

        ' jika valid
        If Validation.isValid Then

            '# insert to table tbl_pendaftaran_smk
            DB.FlushCache()
            DB.Insert("tbl_pendaftaran")
            DB.SetField("tanggal_pendaftaran", DateSafe(txt_tgl_pendaftaran.Text))
            DB.SetField("asal_pendaftar", cmb_asal_pendaftar.SelectedIndex)
            DB.SetField("no_pendaftaran", txt_no_pendaftar_01.Text & "-" & txt_no_pendaftar_02.Text)
            DB.SetField("tahun_lulus", cmb_tahun_lulus.Text)
            DB.SetField("no_uan", mask_no_un.Text)
            DB.SetField("nama_pendaftar", txt_nama_pendaftar.Text)
            DB.SetField("jenis_kelamin", IIf(cmb_jenis_kelamin.SelectedIndex = 0, "L", "P"))
            DB.SetField("tempat_lahir", txt_tempat_lahir.Text)
            DB.SetField("tanggal_lahir", DateSafe(mask_tanggal_lahir.Text))
            DB.SetField("asal_sekolah", txt_asal_sekolah.Text)
            DB.SetField("n_indonesia", NilaiSafe(txt_n_indonesia.Text))
            DB.SetField("n_inggris", NilaiSafe(txt_n_inggris.Text))
            DB.SetField("n_matematika", NilaiSafe(txt_n_matematika.Text))
            DB.SetField("n_ipa", NilaiSafe(txt_n_ipa.Text))
            DB.SetField("n_agama", NilaiSafe(txt_n_agama.Text))
            DB.SetField("n_ips", NilaiSafe(txt_n_ips.Text))
            DB.SetField("n_pkn", NilaiSafe(txt_n_pkn.Text))

            DB.SetField("n_total", NilaiSafe(txt_n_total.Text))
            DB.SetField("pilihan_1", ListSekolah.getIndex(cmb_pilihan_1.Text, cmb_jurusan_1.Text))
            DB.SetField("pilihan_2", ListSekolah.getIndex(cmb_pilihan_2.Text, cmb_jurusan_2.Text))
            DB.SetField("status", "0")

            If Connection.ExecuteNonQuery(DB.GetQueryString) Then
                ' Inser Code Here
                MsgBox("Data berhasil disimpan ke dalam database", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")

                '# kosongkan semua field
                Call bersihkan()
            End If

        Else
            Validation.showMessage()
        End If

    End Sub

    Private Sub frm_smk_add_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        txt_tgl_pendaftaran.Text = FormatDateTime(Now, DateFormat.ShortDate)
        txt_no_pendaftar_01.Text = APP.Info.KD_Sekolah
        cmb_asal_pendaftar.SelectedIndex = 0
        cmb_tahun_lulus.SelectedIndex = 0
        cmb_jenis_kelamin.SelectedIndex = 0

        ' masukan List sekolah
        With ListSekolah

            '# pilihan 1
            cmb_pilihan_1.Items.Clear()
            For Me.i = 0 To .List.Count - 1
                cmb_pilihan_1.Items.Add(.getNama(APP.Info.KD_Sekolah))
            Next
            cmb_pilihan_1.SelectedIndex = 0

            '# jurusan 1
            cmb_jurusan_1.Items.Clear()
            For Me.i = 0 To .List.Count - 1
                If .List.Item(Me.i).Kode = APP.Info.KD_Sekolah Then
                    cmb_jurusan_1.Items.Add(.List.Item(Me.i).Jurusan)
                End If
            Next
            cmb_jurusan_1.SelectedIndex = 0


            '# pilihan 2
            cmb_pilihan_2.Items.Clear()
            For Me.i = 0 To .List.Count - 1
                If Me.i <> 0 Then
                    If .List.Item(Me.i - 1).Kode <> .List.Item(Me.i).Kode Then
                        cmb_pilihan_2.Items.Add(.List.Item(Me.i).Nama)
                    End If
                Else
                    cmb_pilihan_2.Items.Add(.List.Item(Me.i).Nama)
                End If
            Next
            cmb_pilihan_2.Text = .getNama(APP.Info.KD_Sekolah)

            '# jurusan 2
            cmb_jurusan_2.Items.Clear()
            For Me.i = 0 To .List.Count - 1
                If .List.Item(Me.i).Kode = APP.Info.KD_Sekolah Then
                    cmb_jurusan_2.Items.Add(.List.Item(Me.i).Jurusan)
                End If
            Next
            cmb_jurusan_2.SelectedIndex = 0

        End With

        '#
        lbl_n_indo.Text = "x " & Pengkali_UN
        lbl_n_mate.Text = "x " & Pengkali_UN
        lbl_n_ingg.Text = "x " & Pengkali_UN
        lbl_n_ipa.Text = "x " & Pengkali_UN

        lbl_n_agama.Text = "x " & Pengkali_US
        lbl_n_pkn.Text = "x " & Pengkali_US
        lbl_n_ips.Text = "x " & Pengkali_US

    End Sub

    Private Sub cmb_pilihan_1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_pilihan_1.SelectedIndexChanged
        'txt_kluster1.Text = ListSekolah.getKluster(cmb_pilihan_1.Text)
    End Sub

    Private Sub cmb_pilihan_2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_pilihan_2.SelectedIndexChanged
        Try
            With ListSekolah
                '# jurusan 2
                cmb_jurusan_2.Items.Clear()
                For Me.i = 0 To .List.Count - 1
                    If .List.Item(Me.i).Nama = cmb_pilihan_2.Text Then
                        cmb_jurusan_2.Items.Add(.List.Item(Me.i).Jurusan)
                    End If
                Next
                cmb_jurusan_2.SelectedIndex = 0
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles txt_n_indonesia.GotFocus, txt_n_inggris.GotFocus, txt_n_ipa.GotFocus, txt_n_matematika.GotFocus, txt_n_agama.GotFocus, txt_n_ips.GotFocus, txt_n_pkn.GotFocus _
                , txt_nama_pendaftar.GotFocus, txt_tempat_lahir.GotFocus, txt_asal_sekolah.GotFocus

        SendKeys.Send("{HOME}+{END}")
    End Sub

    Private Sub txt_n_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
        Handles txt_n_indonesia.KeyPress, txt_n_inggris.KeyPress _
                , txt_n_ipa.KeyPress, txt_n_matematika.KeyPress

        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) And Not e.KeyChar = "," Then
            e.Handled = True
            Beep()
        End If
    End Sub


    Private Sub txt_n_indonesia_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_indonesia.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_indonesia.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_indonesia.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_indonesia.Text = "0,00"
        End Try

    End Sub

    Private Sub txt_n_inggris_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_inggris.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_inggris.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_inggris.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_inggris.Text = "0,00"
        End Try
    End Sub

    Private Sub txt_n_ipa_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_ipa.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_ipa.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_ipa.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_ipa.Text = "0,00"
        End Try
    End Sub

    Private Sub txt_n_matematika_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_matematika.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_matematika.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_matematika.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_matematika.Text = "0,00"
        End Try
    End Sub

    Private Sub txt_n_agama_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_agama.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_agama.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_agama.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_agama.Text = "0,00"
        End Try
    End Sub
    Private Sub txt_n_ips_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_ips.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_ips.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_ips.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_ips.Text = "0,00"
        End Try
    End Sub
    Private Sub txt_n_pkn_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_n_pkn.LostFocus
        Try
            Dim nilai As Double = CDbl(txt_n_pkn.Text)

            If nilai > 10 Then
                MsgBox("Nilai tidak boleh lebih dari 10", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Pesan")
                nilai = 0
            End If

            txt_n_pkn.Text = FormatNumber(nilai, 2)

        Catch ex As Exception
            txt_n_pkn.Text = "0,00"
        End Try
    End Sub

    Private Sub txt_n_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles txt_n_indonesia.TextChanged _
                , txt_n_inggris.TextChanged _
                , txt_n_ipa.TextChanged _
                , txt_n_ips.TextChanged _
                , txt_n_pkn.TextChanged _
                , txt_n_agama.TextChanged _
                , txt_n_matematika.TextChanged

        Call HitungNilai()
    End Sub

    Private Sub txt_no_pendaftar_02_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_no_pendaftar_02.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
            Beep()
        End If
    End Sub

    Private Sub cmd_batal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_batal.Click
        Me.Hide()
    End Sub

End Class